<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
    body
    {
        overflow-x: hidden;
        padding: 5px;
        margin: 0;
        margin-right: 15px;
        font-size: 13px;
        line-height: 1.5em;
        background: #fff;
        color: #454545;
        width:750px;
        height:750px;
    }
    
    a
    {
        color: #4A8EBC;
        background: inherit;
    }
    
    a:hover
    {
        color: #C3593C;
        background: inherit;
    }
    
    a.title
    {
        color: #FE6700;
        background: #FFF;
    }
    
    #ok_button
    {
        display:block;
        width:50px;
        }
        
        
    
    #url_text
    {
        width:500px;
        height:200px;
    }
    li span.title{width:400px;}
    li span.status{width:100px;}
    li span.result{width:200px;}
    li span{display:inline-block;}
    
</style>
<div>
    <p>请输入帖子的地址，每行一个</p>
    <textarea id="url_text">http://www.xici.net/#d172772939.htm</textarea>
    <input type="button" value="开始" id="ok_button" />
</div>


<table border="1" style="width:700px;">
<thead>
<tr>
    <th style="width:400px;">帖子</th>
    <th style="width:100px;">热度</th>
    <th style="width:100px;">回复</th>
</tr>
</thead>
<tbody id="results">

</tbody>

</table>

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
    var baseUrl = "http://www.xici.net";
    $("#ok_button").click(function () {
        var urlText = $("#url_text").val();        
        var urlArr = urlText.split(/\r?\n/);        
        go(urlArr);
    });
    var urlArr = [];
    function go(url_text_arr) {

        $.each(url_text_arr, function (i, v) {
            urlArr[i] = {};
            urlArr[i]['success'] = false;
            urlArr[i]['url'] = getNewUrl($.trim(v));
            urlArr[i]['id'] = getIdByUrl(urlArr[i]['url']);
        });
        $("#results").empty();
        $.each(urlArr, function (i, v) {
            var tr = $('<tr><td><a></a></td><td></td><td></td></tr>');
            tr.attr("id", v['id']);
            tr.find("td:eq(0) a:eq(0)").text(v['url']).attr('href', v['url']);
            $("#results").append(tr);
        })
        $("#results").show();
        getResult();
    }

    function getResult() {
        if (urlArr && urlArr.length > 0) { 
            var tiezi = urlArr.shift();
            var id = tiezi['id'];
            var url = tiezi['url'];

            $.ajax({ url: url,
                type: 'GET',
                dataType: 'html',
                complete: function (response, status) {
                    var $response = $(response.responseText);                    
                    if (response.status == 404 && response.statusText=="NOT FOUND") {
                        tiezi['title'] = "帖子已删除"; ;
                        tiezi['comments'] = "帖子已删除";
                        setLi(tiezi);
                        window.setTimeout(getResult, 500);
                        return;
                    }
                    tiezi['title'] = $response.find("h1:first").text();
                    tiezi['comments'] = $response.find("div.doc_sp").length;

                    var url = $response.find("#doc_tit #shareBlock1").next("span").find("a:eq(0)").attr("href");
                    var url = baseUrl + url;

                    setLi(tiezi);
                    var bid = getBIdByUrl(url);
                    $.getJSON("http://www.xici.net/api.asp?method=xici.board.popularity&bid=" + bid + "&ids=" + id, function (r) {
                        if (r['Error']) {
                            tiezi['hot'] = '获取失败！';
                        }
                        else {
                            tiezi['hot'] = r['Result'][0];
                        }

                        setLi(tiezi);
                        window.setTimeout(getResult, 500);

                    });
                }
            });       
        }
    }

    function getInfoByResponse() { 
    
    }

    function setLi(obj) {
        var node_id = "#" + obj['id'];
        var node = $(node_id);
        node.find("a").text(obj['title']);
        var result = "查询中...";
        if (obj['hot']) {
            node.find("td:eq(1)").text( obj['hot']);
        }
        if (obj['comments'] || obj['comments'] === 0) {
            node.find("td:eq(2)").text(obj['comments']);            
        }        
    }
   

    function getIdByUrl(url) {
        url = url.split("/d");
        url = url[url.length - 1];
        url = url.split(".");
        url = url[0];
        
        return url;
    }

    function getBIdByUrl(url)
    {
        url = url.split("/b");
        url = url[url.length - 1];        
		url = url.replace(/\//,'')
        return url;
    }

    function getNewUrl(oldUrl) {
        var newUrl = oldUrl.replace(/#/, '');
        return newUrl;
    }
    
    

</script>
